﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SqliteLogViewer {
    public partial class FrmAbout : Form {
        public FrmAbout() {
            InitializeComponent();
        }

        private void FrmAbout_Load(object sender, EventArgs e) {
            this.Text = $"关于 {Application.ProductName}";
            this.label1.Text = $"{Application.ProductName} {Application.ProductVersion}";
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e) {
            // 定义软件信息
            string owner = "EggStudio";
            string name = Application.ProductName;
            string fileName = $"{name}.LogFile";
            string exeName = "SqliteLogViewer.exe";
            string extName = ".dlog";
            string iconName = "log.ico";
            string description = "Sqlite Log File";
            // 注册软件路径
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
                //it.DebugInfo("软件注册中 ...");
                // Windows
                var identity = WindowsIdentity.GetCurrent();
                var principal = new WindowsPrincipal(identity);
                var isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
                if (isElevated) {
                    //it.DebugInfo("添加应用程序信息中 ...");
                    var keySoftware = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
                    var keyCompany = keySoftware.OpenSubKey(owner, true);
                    if (eggs.Object.IsNull(keyCompany)) {
                        keyCompany = keySoftware.CreateSubKey(owner);
                    }
                    var keyCompanyProduct = keyCompany.OpenSubKey(name, true);
                    if (eggs.Object.IsNull(keyCompanyProduct)) {
                        keyCompanyProduct = keyCompany.CreateSubKey(name);
                    }
                    keyCompanyProduct.SetValue("Path", $"{eggs.Assembly.ExecutionFilePath}{exeName}");
                    //it.DebugInfo("添加关联文件信息中 ...");
                    // 建立关联产品
                    var keyProduct = Registry.ClassesRoot.OpenSubKey(fileName, true);
                    if (eggs.Object.IsNull(keyProduct)) {
                        keyProduct = Registry.ClassesRoot.CreateSubKey(fileName);
                    }
                    keyProduct.SetValue("", description);
                    // 建立关联图标
                    var keyProductDefaultIcon = keyProduct.OpenSubKey("DefaultIcon", true);
                    if (eggs.Object.IsNull(keyProductDefaultIcon)) {
                        keyProductDefaultIcon = keyProduct.CreateSubKey("DefaultIcon");
                    }
                    keyProductDefaultIcon.SetValue("", $"\"{eggs.Assembly.ExecutionFilePath}{iconName}\"");
                    // 建立shell
                    var keyProductShell = keyProduct.OpenSubKey("shell", true);
                    if (eggs.Object.IsNull(keyProductShell)) {
                        keyProductShell = keyProduct.CreateSubKey("shell");
                    }
                    // 建立open
                    var keyProductShellOpen = keyProductShell.OpenSubKey("open", true);
                    if (eggs.Object.IsNull(keyProductShellOpen)) {
                        keyProductShellOpen = keyProductShell.CreateSubKey("open");
                    }
                    // 建立command
                    var keyProductShellOpenCommand = keyProductShellOpen.OpenSubKey("command", true);
                    if (eggs.Object.IsNull(keyProductShellOpenCommand)) {
                        keyProductShellOpenCommand = keyProductShellOpen.CreateSubKey("command");
                    }
                    keyProductShellOpenCommand.SetValue("", $"\"{eggs.Assembly.ExecutionFilePath}{exeName}\" --file:\"%1\"");
                    // 建立关联扩展名
                    var keyFile = Registry.ClassesRoot.OpenSubKey(extName, true);
                    if (eggs.Object.IsNull(keyFile)) {
                        keyFile = Registry.ClassesRoot.CreateSubKey(extName);
                    }
                    // 建立关联扩展名打开方式
                    var keyFileOpenWithProgids = keyFile.OpenSubKey("OpenWithProgids", true);
                    if (eggs.Object.IsNull(keyFileOpenWithProgids)) {
                        keyFileOpenWithProgids = keyFile.CreateSubKey("OpenWithProgids");
                    }
                    keyFileOpenWithProgids.SetValue(fileName, "");
                    MessageBox.Show("注册成功!", Application.ProductName);
                } else {
                    MessageBox.Show("请使用管理员权限运行", Application.ProductName);
                }
            } else {
                MessageBox.Show($"不支持的操作系统'{RuntimeInformation.OSDescription}'", Application.ProductName);
            }
        }
    }
}
